package vn.team.clipxonxao.adapter;

import java.util.List;

import vn.team.clipxonxao.model.VideoClip;
import vn.team.clipxonxao.ui.R;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mk.fw.imageloader.MKImageFetcher;

public class HomeClipAdaper extends BaseAdapter{

	private List<VideoClip> listVideo;
	private FragmentActivity mContext;
	private LayoutInflater inflater;
	private ViewHolder viewHolder;
	private MKImageFetcher imageLoader;
	
	public HomeClipAdaper(FragmentActivity mContext,
			List<VideoClip> listVideo, MKImageFetcher imageLoader){
		this.mContext = mContext;
		this.listVideo = listVideo;
		this.inflater = LayoutInflater.from(this.mContext);
		this.imageLoader = imageLoader;
	}
	
	@Override
	public int getCount() {
		return listVideo.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		viewHolder = new ViewHolder();
		if(convertView == null){
			convertView = inflater.inflate(R.layout.layout_content_items, null, false);
			viewHolder.avatar = (ImageView)convertView.findViewById(R.id.layout_content_items_avatar);
			viewHolder.nameVideo = (TextView)convertView.findViewById(R.id.layout_content_items_nameVideo);
			viewHolder.timeVideo = (TextView)convertView.findViewById(R.id.layout_content_items_timeVideo);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		try {
			VideoClip clipVideo = listVideo.get(position);
			imageLoader.getImageFetcher().loadImage(clipVideo.getImg(), viewHolder.avatar);
			viewHolder.nameVideo.setText(clipVideo.getTitle());
			viewHolder.timeVideo.setText(clipVideo.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return convertView;
	}

	
	static class ViewHolder{
		public ImageView avatar;
		public TextView nameVideo;
		public TextView timeVideo;
	}
}
